package org.ksug.samples.springmvc;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class AccountController extends MultiActionController {

	private static final String PARAM_NUMBER = "number";

	private AccountRepository repository;

	public ModelAndView show(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView("accounts/show")
				.addObject(repository
				.findAccount(ServletRequestUtils.getStringParameter(request,
						PARAM_NUMBER)));
	}

	public ModelAndView list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		return new ModelAndView("accounts/list")
				.addObject("accounts", repository.list());
	}
	
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		repository.delete(ServletRequestUtils.getStringParameter(request, PARAM_NUMBER));
		return new ModelAndView("redirect:/accounts/list.do");
	}

	public void setRepository(AccountRepository repository) {
		this.repository = repository;
	}

}